package com.commerce.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.commerce.common.entity.Cart;


import com.commerce.common.entity.Product;
import com.commerce.common.exception.BusinessException;
import com.commerce.order.mapper.CartMapper;
import com.commerce.order.service.CartService;


import com.commerce.product.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    @Autowired
    private ProductService productService;

    /**
     * 添加商品到购物车
     */
    @Override
    public Cart addToCart(Cart cart) {
        // 检查商品是否存在
        Product product = productService.getProductById(cart.getProductId());
        if (product == null) {
            throw new BusinessException("商品不存在");
        }
        
        // 检查商品库存
        if (product.getStock() < cart.getQuantity()) {
            throw new BusinessException("商品库存不足");
        }
        
        // 检查该商品是否已在购物车中
        Cart existingCart = checkProductInCart(cart.getUserId(), cart.getProductId());
        if (existingCart != null) {
            // 更新数量
            existingCart.setQuantity(existingCart.getQuantity() + cart.getQuantity());
            existingCart.setUpdateTime(LocalDateTime.now());
            updateById(existingCart);
            return existingCart;
        }
        
        // 设置商品信息
        cart.setProductName(product.getName());
        cart.setProductImage(product.getMainImage());
        cart.setPrice(product.getPrice().doubleValue());
        cart.setCreateTime(LocalDateTime.now());
        
        // 保存购物车
        save(cart);
        
        return cart;
    }

    /**
     * 更新购物车商品数量
     */
    @Override
    public Cart updateCartQuantity(Long id, Integer quantity) {
        // 获取购物车项
        Cart cart = getById(id);
        if (cart == null) {
            throw new BusinessException("购物车项不存在");
        }
        
        // 检查商品库存
        Product product = productService.getProductById(cart.getProductId());
        if (product == null) {
            throw new BusinessException("商品不存在");
        }
        
        if (product.getStock() < quantity) {
            throw new BusinessException("商品库存不足");
        }
        
        // 更新数量
        cart.setQuantity(quantity);
        cart.setUpdateTime(LocalDateTime.now());
        updateById(cart);
        
        return cart;
    }

    /**
     * 清空用户购物车
     */
    @Override
    public void clearCart(Long userId) {
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getUserId, userId);
        remove(queryWrapper);
    }

    /**
     * 获取用户购物车列表
     */
    @Override
    public List<Cart> getUserCart(Long userId) {
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getUserId, userId);
        queryWrapper.orderByDesc(Cart::getCreateTime);
        return list(queryWrapper);
    }

    /**
     * 获取用户购物车商品数量
     */
    @Override
    public Integer getCartCount(Long userId) {
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getUserId, userId);
        return Math.toIntExact(count(queryWrapper));
    }

    /**
     * 检查商品是否在购物车中
     */
    @Override
    public Cart checkProductInCart(Long userId, Long productId) {
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getUserId, userId);
        queryWrapper.eq(Cart::getProductId, productId);
        return getOne(queryWrapper);
    }



}